package com.industrial.im.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.industrial.common.core.domain.R;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.im.config.RongCloudConfig;
import com.industrial.im.domain.ImFrequentContacts;
import com.industrial.im.mapper.ImFrequentContactsMapper;
import com.industrial.im.mapper.ImUserMapper;
import com.industrial.im.service.ImUserService;
import com.industrial.system.api.RemoteUserService;
import com.industrial.system.api.domain.SysDept;
import com.industrial.system.api.domain.SysUser;
import io.rong.RongCloud;
import io.rong.methods.user.User;
import io.rong.models.response.TokenResult;
import io.rong.models.user.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ImUserServiceImpl implements ImUserService {

    @Autowired
    private ImUserMapper imUserMapper;

    @Autowired
    private RongCloud rongCloud;

    @Autowired
    private ImFrequentContactsMapper imFrequentContactsMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 注册用户换取Token
     *
     * @param params
     */
    @Override
    public JSONObject registerUserInfoToRongCloud(Map<String, String> params) {

        User user = rongCloud.user;

        UserModel userModel = new UserModel();
        userModel.setId(params.get("userId"));
        userModel.setName(params.get("userName"));
        try {
            TokenResult result = user.register(userModel);
            String token = result.toString();
            if(StringUtils.isNotBlank(token)){
                JSONObject jsonObject = JSONObject.parseObject(token);
                return jsonObject;
            }
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @param fcId
     */
    @Override
    public void setFrequentContacts(String fcId) {
        String currentLoginUserId = SecurityUtils.getUserId();
        ImFrequentContacts params = new ImFrequentContacts();
        params.setBaseId(IdUtil.simpleUUID());
        params.setUserId(currentLoginUserId);
        params.setFcId(fcId);
        params.setBaseCreateTime(DateUtils.getNowDate());
        params.setBaseCreateBy(currentLoginUserId);
        imFrequentContactsMapper.insertFrequentContacts(params);
    }

    /**
     *
     */
    @Override
    public List<ImFrequentContacts> loadFcUserList() {
        String currentLoginUserId = SecurityUtils.getUserId();
        List<ImFrequentContacts> result = imFrequentContactsMapper.selectFcUserListByUserId(currentLoginUserId);
        if(result != null && !result.isEmpty()){
            List<String> fcIdList = result.stream().map(ImFrequentContacts::getFcId).collect(Collectors.toList());
            R<List<SysUser>> listR = remoteUserService.queryUserInfoByUserIds(fcIdList);
            if(listR.getCode() == R.SUCCESS){
                List<SysUser> data = listR.getData();
                Map<String, SysUser> fcUserInfoMap = data.stream().collect(Collectors.toMap(SysUser::getUserId, v -> v));
                for(ImFrequentContacts temp : result){
                    String fcId = temp.getFcId();
                    SysUser fcUserInfo = fcUserInfoMap.get(fcId);
                    if(fcUserInfo != null){
                        temp.setFcName(fcUserInfo.getNickName());
                        SysDept dept = fcUserInfo.getDept();
                        temp.setDeptName(dept == null ? "" : dept.getDeptName());
                    }
                }
            }
        }
        return result;
    }

    /**
     * 取消常用联系人
     *
     * @param fcId
     */
    @Override
    public void cacalFcUser(String fcId) {
        String currentLoginUserId = SecurityUtils.getUserId();
        ImFrequentContacts removeParams = new ImFrequentContacts();
        removeParams.setUserId(currentLoginUserId);
        removeParams.setFcId(fcId);
        imFrequentContactsMapper.cacalFcUser(removeParams);
    }
}
